import * as ValidHelp from '../../helps/valid';
import * as robotModel from '../../models/robot';
import * as robotRelationModel from '../../models/robot_relation';
import * as userModel from '../../models/user';
import * as userChatModel from '../../models/user_chat';
import { ValidException, EmptyException, SystemException } from '../../helps/exception';

export function* getRobotId({ user_id }) {
  if (!ValidHelp.Required(user_id)) throw ValidException('用户为空');
  let user = yield userModel.get(user_id);
  return {
    robot_id: user.robot_id,
  };
}

export function* bind({ code, user_id }) {
  if (!ValidHelp.Required(code)) throw ValidException('机器码为空');
  if (!ValidHelp.Required(user_id)) throw ValidException('用户为空');
  let robot = yield robotModel.getByCode(code);
  if (!robot) throw EmptyException('机器码错误');
  let user = yield userModel.get(user_id);
  if (!user) throw EmptyException('用户错误');
  // 先解除当前绑定
  if (user.robot_id) {
    yield robotRelationModel.unbind(user.robot_id, user.id);
  }
  yield user.bindRobot(robot.id);
  let relation = yield robotRelationModel.bind(robot.id, user.id, user.type);
  if (!relation) throw SystemException('机器绑定失败');
  return robot.info();
}

export function* getInfo({ robot_id }) {
  if (!ValidHelp.Required(robot_id)) throw ValidException('机器未绑定');
  let robot = yield robotModel.get(robot_id);
  if (!robot) throw EmptyException('机器不存在');

  return robot.info();
}

export function* putInfo({ avatar, nickname, birthday, gender, robot_id }) {
  if (!ValidHelp.Required(robot_id)) throw ValidException('机器未绑定');
  let robot = yield robotModel.get(robot_id);
  yield robot.put({ avatar, nickname, birthday, gender });
  return robot.info();
}

export function* getNight({ robot_id }) {
  if (!ValidHelp.Required(robot_id)) throw ValidException('机器未绑定');
  let robot = yield robotModel.get(robot_id);
  return {
    is_open: robot.night_status,
    start_time: robot.night_start_time,
    end_time: robot.night_end_time,
  };
}

export function* putNight({ robot_id, is_open, start_time, end_time }) {
  if (!ValidHelp.Required(robot_id)) throw ValidException('机器未绑定');
  let robot = yield robotModel.get(robot_id);
  yield robot.put({
    night_status: is_open,
    night_start_time: new Date(start_time),
    night_end_time: new Date(end_time),
  });
  return {};
}

export function* getAntiAddiction({ robot_id }) {
  if (!ValidHelp.Required(robot_id)) throw ValidException('机器未绑定');
  let robot = yield robotModel.get(robot_id);
  return {
    is_open: robot.anti_addiction_status,
    duration: robot.anti_addiction_duration,
  };
}

export function* putAntiAddiction({ robot_id, is_open, duration }) {
  if (!ValidHelp.Required(robot_id)) throw ValidException('机器未绑定');
  let robot = yield robotModel.get(robot_id);
  yield robot.put({
    anti_addiction_status: is_open,
    anti_addiction_duration: duration,
  });
  return {};
}

export function* getChat({ robot_id, user_id, max_id, feature, number }) {
  if (!ValidHelp.Required(user_id)) throw ValidException('用户为空');
  if (!ValidHelp.Integer(number)) throw ValidException('消息树为空');
  if (!ValidHelp.Required(robot_id)) throw ValidException('机器未绑定');
  if (!ValidHelp.Required(feature)) throw ValidException('功能设定为空');
  let list = yield userChatModel.listByLast(robot_id, user_id, max_id, feature, number);
  return list;
}
